﻿<%@ Page Language="C#" MasterPageFile="~/StaffIntranet.Master" AutoEventWireup="true" CodeBehind="Locations.aspx.cs" Inherits="MCromwell.StaffIntranet.UI.maintenance.Locations" Title="Manage Locations" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="ContentPlaceHolder" runat="server">
    <form id="frmEdit" method="post" runat="server">
        <h1>Manage Locations</h1>
    
    <asp:CustomValidator ID="valForeignKeyConstraint" runat="server" ErrorMessage="Location could not be deleted because it is referenced to one or more staff."></asp:CustomValidator>
    
    <asp:GridView ID="grdLocations" runat="server" AutoGenerateColumns="False" 
            DataSourceID="LocationDataSource" DataKeyNames="Id" CssClass="grid" 
            ShowFooter="True" onrowcommand="grdLocations_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="Id" SortExpression="Id" InsertVisible="False">
                <FooterTemplate>
                    <asp:Button ID="btnAdd" CausesValidation="true" runat="server" Text="Add" CommandName="New" />
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Value" 
                SortExpression="Value">
                <EditItemTemplate>
                    <asp:TextBox ID="txtValue" runat="server" Text='<%# Bind("Value") %>'></asp:TextBox>
                    
                            <asp:RequiredFieldValidator ID="valValueRequired" runat="server" 
            ErrorMessage="Value must be supplied." 
            ValidationGroup="LocationValidation" ControlToValidate="txtValue" Display="Dynamic"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtNewValue" Text='<%# Bind("Value") %>' runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="valValueRequired" runat="server" 
                        ControlToValidate="txtNewValue" Display="Dynamic" 
                        ErrorMessage="Value must be supplied." ></asp:RequiredFieldValidator>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblValue" runat="server" Text='<%# Bind("Value") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ButtonType="Button" 
                ValidationGroup="LocationValidation" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="LocationDataSource" runat="server" 
            ProviderName="System.Data.SqlClient" 
            SelectCommand="SELECT [Id], [Value] FROM [Location]"  
            UpdateCommand="UPDATE Location SET [Value] = @Value WHERE [Id] = @Id" 
            DeleteCommand="DELETE FROM Location WHERE [Id] = @Id" 
            ondeleted="LocationDataSource_Deleted" InsertCommand="DECLARE @NextId INT;
            
SET @NextId = (SELECT MAX(Id) FROM Location) + 1;
INSERT INTO Location VALUES (@NextId, @Value)" 
            oninserting="LocationDataSource_Inserting"></asp:SqlDataSource>
    </form>
</asp:Content>

